package org.zhaya.sdk.constant;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @Author Ryan Yang
 * @Date 2022/10/10 4:50 下午
 */
@Slf4j
public enum PipelineSawmillTypeEnum {
    NODE {
        @Override
        public String template() {
            return getTemplate("node.btl");
        }
    },
    STEPS {
        @Override
        public String template() {
            return getTemplate("steps.btl");
        }
    },
    SWITCH {
        @Override
        public String template() {
            return getTemplate("switch.btl");
        }
    };

    protected String template;

    private static final String templatePackage = "/template/";

    public abstract String template();

    protected String getTemplate(String subName) {
        InputStream resourceAsStream = null;
        if (StringUtils.isNotBlank(this.template)) {
            return this.template;
        }
        try {
            resourceAsStream = this.getClass().getResourceAsStream(templatePackage + subName);
            this.template = IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("parse steps beetl config error {}", e.getMessage());
            return null;
        } finally {
            if (resourceAsStream != null) {
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    log.error("PipelineSawmillTypeEnum close resourceAsStream error {},{}", subName, e.getMessage());
                }
            }
        }
        return this.template;
    }
}
